package com.sqznkj.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sqznkj.Service.AdminService;
import com.sqznkj.Service.ComInfoService;
import com.sqznkj.entity.artcle;
import com.sqznkj.entity.comInfo;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Controller
public class login {
    @Resource
    AdminService service;
    @Resource
    ComInfoService comInfoService;
    @RequestMapping("/checkUser")
    /**
     * 验证登陆
     */
     public String check(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("userName");
        String password = request.getParameter("userPwd");
        boolean isExist = false;
        if(service.checkName().getUsername().equals(username) &&service.checkName().getPassword().equals(password)){
            return "adminPage";
        }
        return "errorPage";

     }
     @RequestMapping("/news0")
     public String new2(Map<String,Object>map){
         comInfo comInfo = comInfoService.getComInfo();
         map.put("comInfo", comInfo);
         return "newsbak";
     }
    @RequestMapping("/news")
     public String new1(HttpServletRequest request, HttpServletResponse response,Map<String,Object> map){
        String t = request.getParameter("title");
        comInfo comInfo = comInfoService.getComInfo();
        List<artcle> list = service.getArtcle();
        for (artcle a:list) {
            if (t.equals(a.getTitle())){
                map.put("comInfo", comInfo);
                map.put("artcle", a);
                return "news";
            }
        }

        return "error1";
    }
    @RequestMapping("/newList")
    public String newlist(Map<String,Object> map){
        map.put("artcles", service.getArtcle());
        map.put("comInfo", comInfoService.getComInfo());
        return "newList";
    }
    @RequestMapping("/gongsList")
    public String gslist(Map<String,Object> map){
        map.put("artcles", service.getArtcle());
        map.put("comInfo", comInfoService.getComInfo());
        return "gongsList";
    }
    @RequestMapping("/dtList")
    public String dtlist(Map<String,Object> map){
        map.put("artcles", service.getArtcle());
        map.put("comInfo", comInfoService.getComInfo());
        return "dtList";
    }
}
